
# 获取营业部排行榜

# 获取前25或者55个营业部的最近3个月的个股，和板块名称


# 主要模块
# 下载网页 requests
# josn 解码
# 数据库 mysql
# 结果展示sankey关系图 ，echarts

import requests
import json
import pymysql
import sqlite3
import time
import hashlib


def get_paihang(pnum=1,ynum=5,tout=500,start=0):
    '''
    http://datainterface.eastmoney.com/EM_DataCenter/JS.aspx?type=LHB&sty=YYTJ&stat=6&sr=0&st=1&p=2&ps=50&js=var%20BTXigdop={%22data%22:[(x)],%22pages%22:%22(pc)%22,%22update%22:%22(ud)%22}&rt=50897252
    '''



    url = "http://datainterface.eastmoney.com/EM_DataCenter/JS.aspx?type=LHB&sty=YYTJ&stat=6&sr=0&st=1&p=1&ps="+str(pnum)+"&js={%22data%22:[(x)],%22pages%22:%22(pc)%22,%22update%22:%22(ud)%22}&rt=50897357"
    html = requests.get(url,timeout=tout)
    # print(html.text)
    json_data = json.loads(html.text)
    # print(json_data['data'])


    data = json_data['data']
    n = 1
    for value in data:
        print(u'当前营业部%d\r\n'%(n))
        # print(value.split(','))
        ones = value.split(',')
        # print(ones)
        if(n>start):
            get_yingyebu(ones[0],ones[12],ynum,tout)
            # cursor.close()
            conn.commit()
        n = n + 1
        pass
    pass

def get_yingyebu(id,name,num,tout):
    # http://data.eastmoney.com/soft/stock/TradeSearchHistory.aspx?id=80601499
    # http://datainterface.eastmoney.com/EM_DataCenter/JS.aspx?type=LHB&sty=ZLYYMM&stat=3&code=80601499&sr=0&st=2&p=1&ps=50&js=var%20mqaFKGQP={%22data%22:[(x)],%22pages%22:%22(pc)%22,%22update%22:%22(ud)%22}
    # url = "http://data.eastmoney.com/soft/stock/TradeSearchHistory.aspx?id=%s"%(id)
    url = 'http://datainterface.eastmoney.com/EM_DataCenter/JS.aspx?type=LHB&sty=ZLYYMM&stat=3&code='+str(id)+'&sr=0&st=2&p=1&ps='+str(num)+'&js={"data":[(x)],"pages":"(pc)","update":"(ud)"}&rt=50897357'
    # print(url,name)
    headers = {'content-type': 'application/json',
           'User-Agent': 'Mozilla/5.0 (X11; Ubuntu; Linux x86_64; rv:22.0) Gecko/20100101 Firefox/22.0'}

    html = requests.get(url,headers=headers,timeout=tout)
    # print(html.text)
    if(html):
        json_data = json.loads(html.text)
        # print(json_data['data'])


        data = json_data['data']
        # print(data)

        time.sleep(3)
        for value in data:
            ones = value.split(',')
            # print(ones)

            src = u'%s%s%s'%(str(ones[6]),str(ones[10]),str(ones[12]))
            print(src)
            m2 = hashlib.md5()
            m2.update(src.encode("utf8"))
            md5=m2.hexdigest()

            print(ones[2],ones[6],ones[10],ones[11],ones[12],ones[13],md5)
            cursor.execute('insert into bankuai (stock_id, stock_name,yyb_name,yyb_id,bk_name,bk_id,hash) values (\'%s\',\'%s\',\'%s\',\'%s\',\'%s\',\'%s\',\'%s\')'%(ones[2],ones[6],ones[10],ones[11],ones[12],ones[13],md5))
            pass

    pass

def get_josn(arg):
    pass

def make_echarts(arg):
    pass


if __name__ == '__main__':
    conn = sqlite3.connect('test.db')
    cursor = conn.cursor()

    try:
        create_tb_cmd='''
        CREATE TABLE IF NOT EXISTS USER
        (NAME TEXT,
        AGE INT,
        SALARY REAL);
        '''

        sql = '''
        CREATE TABLE IF NOT EXISTS bankuai
        (stock_id varchar(20),
        stock_name varchar(20),
        yyb_id varchar(20),
        yyb_name varchar(32),
        bk_id varchar(20),
        bk_name varchar(20),
        hash varchar(32) UNIQUE)
        '''
        #主要就是上面的语句
        # cursor.execute('create table bankuai (stock_id varchar(20), stock_name varchar(20),yyb_id varchar(20),yyb_name varchar(32),bk_id varchar(20),bk_name varchar(20),hash varchar(32))')
        cursor.execute(sql)

    except:
        print("Create table failed")
        # return False


    get_paihang(55,255,5000,31)

    conn.close()
